import { _decorator, Component, Label, director, Node, Color } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 用户ID标签组件
 * 这个组件需要添加到您在Home界面创建的IDlabel节点上
 */
@ccclass('HomeIDLabel')
export class HomeIDLabel extends Component {
    private static instance: HomeIDLabel = null;
    private label: Label = null;
    private currentUserId: string = "未知";

    @property(Label)
    idLabel: Label = null; // 在编辑器中关联您的Label组件

    protected onLoad(): void {
        console.log('HomeIDLabel: 组件加载中...');
        
        // 设置为全局单例，便于其他脚本调用
        HomeIDLabel.instance = this;
        
        // 获取Label组件
        this.label = this.idLabel || this.getComponent(Label) || this.getComponentInChildren(Label);
        
        if (this.label) {
            console.log('HomeIDLabel: 找到Label组件');
            this.label.string = `ID: ${this.currentUserId}`;
        } else {
            console.error('HomeIDLabel: 未找到Label组件，请在编辑器中关联idLabel属性');
        }
        
        console.log('HomeIDLabel: Home界面ID标签初始化完成');
        
        // 延迟检查是否已有用户ID数据
        this.scheduleOnce(() => {
            this.checkForExistingUserID();
        }, 0.5);
    }
    
    /**
     * 检查是否已有用户ID数据
     */
    private checkForExistingUserID(): void {
        // 尝试从MainDian获取已存在的用户ID
        const mainDianInstance = (window as any).MainDianInstance;
        if (mainDianInstance && typeof mainDianInstance.getCurrentUserId === 'function') {
            const existingUserId = mainDianInstance.getCurrentUserId();
            if (existingUserId && existingUserId !== "未知") {
                console.log(`HomeIDLabel: 发现已存在的用户ID: ${existingUserId}`);
                this.updateUserID(existingUserId);
            }
        }
    }

    protected onDestroy(): void {
        if (HomeIDLabel.instance === this) {
            HomeIDLabel.instance = null;
        }
    }

    /**
     * 获取单例实例
     */
    public static getInstance(): HomeIDLabel {
        return HomeIDLabel.instance;
    }

    /**
     * 更新用户ID显示
     * @param userId 用户ID
     */
    public updateUserID(userId: string): void {
        console.log(`HomeIDLabel: 收到用户ID更新请求: ${userId}`);
        this.currentUserId = userId;
        
        if (this.label) {
            this.label.string = `ID: ${userId}`;
            console.log(`HomeIDLabel: 用户ID已更新为 ${userId}`);
            console.log(`HomeIDLabel: 节点激活状态: ${this.node.active}`);
            console.log(`HomeIDLabel: 节点名称: ${this.node.name}`);
        } else {
            console.error('HomeIDLabel: Label组件不存在，无法更新用户ID');
        }
    }

    /**
     * 显示标签
     */
    public show(): void {
        this.node.active = true;
        console.log('HomeIDLabel: 标签已显示');
    }

    /**
     * 隐藏标签
     */
    public hide(): void {
        this.node.active = false;
        console.log('HomeIDLabel: 标签已隐藏');
    }

    /**
     * 设置标签文本颜色
     */
    public setColor(r: number, g: number, b: number, a: number = 255): void {
        if (this.label) {
            this.label.color = new Color(r, g, b, a);
        }
    }

    /**
     * 设置字体大小
     */
    public setFontSize(size: number): void {
        if (this.label) {
            this.label.fontSize = size;
        }
    }
}
